//
//  GenderView.m
//  APP_login
//
//  Created by edz on 7/9/21.
//  Copyright © 2021 baicai. All rights reserved.
//

#import "GenderView.h"
#import "Masonry.h"


@interface GenderView ()
<
UIPickerViewDelegate,
UIPickerViewDataSource
>

@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIButton *finishButton;
@property (nonatomic, strong) UIPickerView *pivkView;
@property (nonatomic, strong) NSString *selectGender;
@end
@implementation GenderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)drawRect:(CGRect)rect {
    // Drawing code
   
}

//UIpikerview
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.3];
        
        _whiteView = [[UIView alloc] init];
        [self addSubview: _whiteView];
        [_whiteView mas_makeConstraints:^(MASConstraintMaker *make){
            make.right.left.mas_equalTo(0);
            make.bottom.mas_equalTo(400);
            make.height.mas_equalTo(400);
        }];
        _whiteView.backgroundColor = [UIColor whiteColor];
        
        _finishButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_whiteView addSubview:_finishButton];
        [_finishButton mas_makeConstraints:^(MASConstraintMaker *make){
            make.right.mas_equalTo(-20);
            make.top.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(60, 30));
        }];
        [_finishButton setTitle:@"完成" forState:UIControlStateNormal];
        _finishButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [_finishButton addTarget:self action:@selector(finishButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _pivkView = [[UIPickerView alloc] init];
        [_whiteView addSubview: _pivkView];
        [_pivkView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.mas_equalTo(30);
            make.left.right.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        _pivkView.delegate = self;
        _pivkView.dataSource = self;
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self hidden];
}

//组数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//每组元素个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (_dataArray.count != 0) {
        return _dataArray.count;
    }
    return 1;
}

//设置每个元素内容 枚举
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _dataArray[row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}

- (void)finishButtonClick{
    [self.delegate genderViewWillRemove:[self.pivkView selectedRowInComponent:0]];
    [self hidden];
}

-(void) setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [_pivkView reloadAllComponents];
}

- (void) hidden{
    [UIView animateWithDuration:0.3 delay:0.3
         options:UIViewAnimationOptionCurveLinear animations:^{
         [self.whiteView mas_updateConstraints:^(MASConstraintMaker *make){
             make.bottom.mas_equalTo(400);
         }];
         [self layoutIfNeeded];
         }
         completion:^(BOOL finished){
                [self removeFromSuperview];
          }
    ];
}

- (void) showView:(UIView*) view{
    [view addSubview:self];
    [UIView animateWithDuration:0.3 delay:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.whiteView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.mas_equalTo(0);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finish){
        
    }];
}
@end
